import { Injectable } from '@nestjs/common';
import { CreateBookInput } from './dto/create-book.input';
import { UpdateBookInput } from './dto/update-book.input';
import { Book } from './entities/book.entity';
import { HasuraService } from '../hasura.service';
import { BookVo } from './vo/book.vo';

@Injectable()
export class BooksService {
  constructor(private readonly hasuraService: HasuraService) {}

  create(createBookInput: CreateBookInput) {
    return 'This action adds a new book';
  }

  async findAll(): Promise<BookVo[]>{
    const query = `
      query MyQuery {
        books {
          author_id
          book_id
          book_title
          publication_year
          author {
            author_name
            author_id
            birth_date
            country
          }
        }
     }
    `;
    const result = await this.hasuraService.query(query, {}).toPromise();
    if (result && result.data && result.data.books) {
      let books: BookVo[] = [];
      books = result.data.books;
      return books;
    } else {
      return [];
    }
  }

  findOne(id: number) {
    return `This action returns a #${id} book`;
  }

  update(id: number, updateBookInput: UpdateBookInput) {
    return `This action updates a #${id} book`;
  }

  remove(id: number) {
    return `This action removes a #${id} book`;
  }
}